﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace AGrandVenture
{
    static class LootManager
    {
        #region Declarations
        public static List<LootItem> OnScreenLoot = new List<LootItem>();
        public static Texture2D lootTexture;
        public static int MaxActiveLoot = 30;
        //System & Diagnostic
        static private Vector2 lastMessageLocation = Vector2.Zero;

        #endregion

        #region Initialise
        public static void Initialise(
            Texture2D texture)
        {
            lootTexture = texture;
        }
        #endregion

        #region Loot Management

        public static void addLoot(LootItem newLoot)
        {
            OnScreenLoot.Add(newLoot);
        }

        #endregion

        #region Update and Draw
        public static void Update(GameTime gameTime)
        {
            for (int x = OnScreenLoot.Count - 1; x >= 0; x--)
            {
                if (Player.BaseSprite.IsCircleColliding(
                                        OnScreenLoot[x].Item.Base.WorldCentre,
                                        OnScreenLoot[x].Item.Base.CollisionRadius))
                {
                    if (Player.Character.addToInventory(OnScreenLoot[x].Item))
                    {
                        MessageWindow.AddMessage("You just looted a " + (OnScreenLoot[x].Item.Name) + "!");
                        OnScreenLoot[x].Destroyed = true;
                        OnScreenLoot[x].Item.Base.Update(gameTime);
                    }
                    else
                    {
                        MessageWindow.AddMessage("You are carrying too much, you leave " + (OnScreenLoot[x].Item.Name) + " behind.");
                        if (lastMessageLocation == Vector2.Zero ||
    lastMessageLocation != Tile_Engine.TileMap.GetCellByPixel(Player.WorldLocation))
                        {
                            lastMessageLocation = Tile_Engine.TileMap.GetCellByPixel(Player.WorldLocation);
                        }
                    }
                }
                if (OnScreenLoot[x].Destroyed)
                {
                    OnScreenLoot.RemoveAt(x);
                }
            }
        }

        public static void Draw(SpriteBatch spriteBatch)
        {
            foreach (LootItem loot in OnScreenLoot)
            {
                loot.Item.Base.Draw(spriteBatch);
            }
        }
        #endregion

    }
}
